<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-06-02
 * Time: 14:31
 */

namespace app\admin\controller;


use app\admin\BaseController;

class OrderController extends BaseController
{
    /**
     * @return \think\response\Json
     */
    public function indexAction()
    {
        try {
            $params = $this->request->get();

            $select = [
                'where' => $this->_get_cond($params),
                'order' => [
                    'orderId' => 'desc'
                ]
            ];

            $orders = $this->app->orderService->getLists($select, $this->page, $this->pagesize);

            $data['lists'] = [];

            if (count($orders)) {
                foreach ($orders as $order) {
                    $data['lists'][] = [
                        'orderId' => $order->orderId,
                        'orderNo' => $order->orderNo,
                        'userId' => $order->userId,
                        'userName' => $order->userName,
                        'totalCredit' => $order->totalCredit,
                        'goodsCount' => count($order->orderGoods),
                        'goodsCategoryId' => $order->orderGoods[0]->goodsCategoryId,
                        'orderStatus' => $order->orderStatus,
                        'orderStatusText' => $order->getOrderStatusText(),
                        'createdAt' => $order->createdAt,
                        'goods' => $this->app->orderService->getGoods($order->orderId)['data']
                    ];
                }
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($orders);

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => [],$e->getLine(),$e->getFile()]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function detailAction()
    {
        try {
            $orderNo = $this->request->get('orderNo');

            $order = $this->app->orderService->getDetailByOrderNo($orderNo);

            if (!$order) {
                throw new \Exception('订单不存在');
            }

            $goodsInfo = $this->app->orderService->getGoods($order->orderId);
            $data = [
                'orderId' => $order->orderId,
                'orderNo' => $order->orderNo,
                'totalCredit' => $order->totalCredit,
                'orderStatus' => $order->orderStatus,
                'orderStatusText' => $order->getOrderStatusText(),
                'createdAt' => $order->createdAt,
                'goods' => $goodsInfo['data'],
                'goodsCategoryId' => $order->orderGoods[0]->goodsCategoryId,
                'consign' => [
                    'consignee' =>$order->express()->value('consignee'),
                    'phone' => $order->express()->value('phone'),
                    'address'=>$order->express && $order->express()->value('provinceCode')?$order->express->province->provinceName.$order->express->city->cityName.$order->express->area->areaName.$order->express->address:'',
                    'number' => $order->express()->value('number'),
                    'idcard' => $order->express()->value('idcard'),
                    'email' => $order->express()->value('email'),
                ],
                'express' => [
                    'expressNo' => $order->express()->value('expressNo')?:'',
                    'expressCompany' => $order->express()->value('expressCompany')?:'',
                    'expressAt' => $order->express()->value('expressAt')?:'',
                ]
            ];

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function expressAction()
    {
        try{
            if (!isset($this->post['orderNo'])) {
                throw new \Exception('订单号不存在', 500);
            }

            $order = $this->app->orderService->getDetailByOrderNo($this->post['orderNo']);

            $this->post['adminId'] = $this->admin->adminId;
            $this->app->orderService->express($order, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    public function exportAction()
    {
        try{
            $params = $this->request->post();

            $select = [
                'where' => $this->_get_cond($params),
                'order' => [
                    'orderId' => 'desc'
                ]
            ];

            $this->app->orderService->export($select);

        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    public function _get_cond($params)
    {
        $cond = [];

        $cond[] = ['appletId', '=', $this->appletId];

        if (isset($params['orderStatus']) && $params['orderStatus'] != '') {
            $cond[] = [
                'orderStatus', '=', $params['orderStatus']
            ];
        } else {
            $cond[] = [
                'orderStatus', '>=', 0
            ];
        }

        if (isset($params['orderNo']) && $params['orderNo'] != '') {
            $cond[] = [
                'orderNo', 'like', '%'.$params['orderNo'].'%'
            ];
        }

        if (isset($params['userName']) && $params['userName'] != '') {
            $cond[] = [
                'userName', 'like', '%'.$params['userName'].'%'
            ];
        }

        if (isset($params['startDate']) && $params['startDate'] != '') {
            $cond[] = [
                'createdAt', '>=', $params['startDate']
            ];
        }

        if (isset($params['endDate']) && $params['endDate'] != '') {
            $cond[] = [
                'createdAt', '<=', $params['endDate'].' 23:59:59'
            ];
        }

        return $cond;
    }
}